const db = wx.cloud.database({
  env: "treeworld-2g5yvz9s899bf4a6"
})
const app = getApp()
Component({


  /**
   * 组件的初始数据
   */
  data: {
    show3: true,
    countDownNum: '1',
    avatarUrl: "https://s1.ax1x.com/2020/07/28/aAdel6.jpg",
    nickName: "游客",
    rankList: [{
      headimgurl: 'http://cdn.zhoukaiwen.com/head4.jpg',
      name: '张 *',
      post: '全栈工程师',
      city: '深圳',
      salary: '17500'
    }, {
      headimgurl: 'http://cdn.zhoukaiwen.com/head5.jpg',
      name: '李 * 阳',
      post: 'Java后台工程师',
      city: '杭州',
      salary: '17000'
    }, {
      headimgurl: 'http://cdn.zhoukaiwen.com/head6.png',
      name: '吕 * 浩',
      post: 'Web前端工程师',
      city: '西安',
      salary: '16000'
    },
    {
      headimgurl: 'http://cdn.zhoukaiwen.com/head7.jpg',
      name: '张 * 鹏',
      post: 'Web前端工程师',
      city: '西安',
      salary: '15500'
    },
    {
      headimgurl: 'http://cdn.zhoukaiwen.com/head8.jpg',
      name: '李 * 静',
      post: 'Web前端工程师',
      city: '西安',
      salary: '15000'
    },
    {
      headimgurl: 'http://cdn.zhoukaiwen.com/head9.jpg',
      name: '周 * 德',
      post: 'Web前端工程师',
      city: '西安',
      salary: '14000'
    },
    {
      headimgurl: 'http://cdn.zhoukaiwen.com/head10.jpeg',
      name: '赵 *',
      post: 'Web前端工程师',
      city: '西安',
      salary: '13500'
    }
  ]
  },

  created() {
    this.sq()
  },

  methods: {
    sz() {

      wx.openSetting({
        success: function (res) {}
      })

    },
    ql() {
      wx.showToast({
        title: '清理缓存成功',
        icon: 'none',
      })
      wx.clearStorage()
    },

    sq2() {
      var user = app.globalData.user
      this.setData({
        list: user
      })
    },
    countDown() {
      let that = this;
      var user = app.globalData.user
      let countDownNum = that.data.countDownNum; //获取倒计时初始值
      //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
      that.setData({
        timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
          //每隔一秒countDownNum就减一，实现同步
          countDownNum--;
          //然后把countDownNum存进data，好让用户知道时间在倒计着
          that.setData({
            countDownNum: countDownNum
          })
          console.log(countDownNum)
          //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
          if (countDownNum == 0) {
            that.setData({
              list: user
            })
            clearInterval(that.data.timer);

          }
          app.globalData.sydh = 2
        }, 1000)
      })
    },
    bindGetUserInfo: function (e) {
      console.log(e.detail.userInfo)
      db.collection('user').add({
        data: {
          avatarUrl: e.detail.userInfo.avatarUrl,
          nickName: e.detail.userInfo.nickName
        }
      }).then(res => {})

      if (e.detail.userInfo) {
        this.sq()

      } else {

      }
    },
    getUserProfile() {
      // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res)

          this.setData({
            avatarUrl: res.userInfo.avatarUrl,
            nickName: res.userInfo.nickName,
            show: false
          })
          db.collection('user').add({
            data: {
              avatarUrl: res.userInfo.avatarUrl,
              nickName: res.userInfo.nickName,
            }
          }).then(res => {})
        }
      })
    },
    sq() {
    
      let that = this
      wx.getStorage({
        key: 'userid',
        success(res) {
          db.collection('user').doc(res.data).get().then(res => {
            that.setData({
              avatarUrl: res.data.avatarUrl,
              nickName: res.data.nickName,
              userin: res.data,
            })
            wx.setStorage({
              key: 'openid',
              data: res.data._openid
            })
            wx.setStorage({
              key: 'userid',
              data: res.data._id
            })
          })
        },
        fail(res2) {
          db.collection('user').get().then(res => {
            console.log(res.data)
            if(res.data.length>0){
              wx.setStorage({
                key: 'openid',
                data: res.data[0]._openid
              })
              that.setData({
                avatarUrl: res.data[0].avatarUrl,
                nickName:  res.data[0].nickName,
                userin:  res.data[0].data,
              })
              wx.setStorage({
                key: 'userid',
                data: res.data[0]._id
              })
            }else{
              that.setData({
                show: true
              })
            }
          })
     
        },
      })



    },

    tz(e) {
      console.log(e.currentTarget.dataset.id)
      wx.navigateTo({
        url: "../../pages/" + e.currentTarget.dataset.a + "?id=" + e.currentTarget.dataset.id
      })
    },

  }
})